//
//  RepeatViewController.m
//  Ringminder
//
//  Created by Think Small Studio on 09-6-20.
//  Copyright 2009 Think Small Studio. All rights reserved.
//

#import "RepeatViewController.h"


@implementation RepeatViewController
@synthesize repeatTableView;
@synthesize editingItem;
@synthesize appHelper;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
		appHelper = [[AppHelper alloc] init];
    }
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Set the title of this view
	self.title = @"Set Repeat";	
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [appHelper.weekdaysList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	cell.textLabel.text = [appHelper.weekdaysList objectAtIndex:indexPath.row];
	
	NSMutableArray *repeat = [editingItem objectForKey:@"repeat"];
	NSNumber *checked = [repeat objectAtIndex:indexPath.row];
	
	if([checked boolValue]) {
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	}
	else {
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	
    return cell;
}

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	[repeatTableView deselectRowAtIndexPath:indexPath animated:NO];
	
	UITableViewCell *cell = [repeatTableView cellForRowAtIndexPath:indexPath];
	
	NSMutableArray *repeat = [editingItem objectForKey:@"repeat"];
	NSNumber *checked = [repeat objectAtIndex:indexPath.row];
	
	if([checked boolValue]) {
		[repeat replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	else {
		[repeat replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	}	
}


- (void)dealloc {
	[repeatTableView release];
	[editingItem release];
	[appHelper release];
    [super dealloc];
}


@end
